import { defineNuxtRouteMiddleware, navigateTo } from "#app";
export default defineNuxtRouteMiddleware((to, from) => {
  // const userAgent = process.server
  //   ? useRequestHeaders()["user-agent"]
  //   : navigator.userAgent;
  const userAgent = import.meta.server
    ? useRequestHeaders()["user-agent"]
    : navigator.userAgent;
  // 正则表达式检测是否为移动端
  const isMobile = /mobile|android|iphone|ipad|ipod/i.test(userAgent);

  if (isMobile && !to.path.startsWith("/m/")) {
    // 移动端请求且当前路径没有 m/ 前缀
    return navigateTo(`/m${to.path}`);
  } else if (!isMobile && to.path.startsWith("/m/")) {
    // PC端请求且当前路径有 m/ 前缀
    return navigateTo(to.path.replace(/^\/m/, ""));
  }
});
